//
//  ImageCollectViewController.m
//  ArtDog
//
//  Created by dllo on 16/9/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ImageCollectViewController.h"
#import "BaseCollectionView.h"
#import "HLMacro.h"
#import "CommunityMainCollectionCell.h"
#import "ImageCollectCollectionCell.h"
#import "CommunityPicture.h"
#import <Photos/Photos.h>

@interface ImageCollectViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) BaseCollectionView *collectionView;

@end

@implementation ImageCollectViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isCollectPage = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)setPictures:(NSArray<CommunityPicture *> *)pictures
{
    _pictures = pictures;
    [_collectionView reloadData];
}

- (void)setImageArray:(NSMutableArray *)imageArray
{
    _imageArray = imageArray;
    [_collectionView reloadData];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:_collectionView];
    flowLayout.itemSize = _collectionView.frame.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _collectionView.backgroundColor = [UIColor blackColor];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageCollectCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageCollectCollectionCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_pictures.count == 0) {
        return _imageArray.count;
    }
    return _pictures.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCollectCollectionCell" forIndexPath:indexPath];
    cell.isCollectPage = _isCollectPage;
    if (_pictures.count == 0) {
        cell.asset = _imageArray[indexPath.item];
        cell.totalPageLabel.text = [NSString stringWithFormat:@"%ld", _imageArray.count];
    } else {
        cell.pictureModel = _pictures[indexPath.item];
        cell.totalPageLabel.text = [NSString stringWithFormat:@"%ld", _pictures.count];
    }
    cell.currentPageLabel.text = [NSString stringWithFormat:@"%ld", indexPath.item + 1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_indexPath.item != 0) {
        collectionView.contentOffset = CGPointMake(_indexPath.item * SCREEN_WIDTH, 0);
        _indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
